package com.yxhl.zoume.core.specialcar.presenter;

import android.content.Context;

import com.yxhl.zoume.common.presenter.BasePresenter;
import com.yxhl.zoume.common.ui.view.BaseView;
import com.yxhl.zoume.core.car.model.PayResultEvent;
import com.yxhl.zoume.core.car.model.StatusEnum;
import com.yxhl.zoume.core.specialcar.view.SpecialCarCashPayView;
import com.yxhl.zoume.data.http.rest.param.specialcar.SpecialCarCashPayParam;
import com.yxhl.zoume.data.http.rest.response.specialcar.SpecialCarCashPayResponse;
import com.yxhl.zoume.data.rxbus.RxBus;
import com.yxhl.zoume.domain.interactor.ZMSubscriber;
import com.yxhl.zoume.domain.interactor.car.specialcar.SpecialCarCashPayUseCase;

import javax.inject.Inject;

/**
 * Created by vhawk on 2016/10/12.
 */
public class SpecialCarCashPayPresenter extends BasePresenter {

    @Inject
    SpecialCarCashPayUseCase useCase;
    private SpecialCarCashPayView view;

    @Inject
    public SpecialCarCashPayPresenter(Context context) {
        super(context);
    }

    @Override
    public void attachView(BaseView view) {
        super.attachView(view);
        this.view = (SpecialCarCashPayView) view;
    }

    /**
     * 乘客现金支付
     *
     * @param orderId
     */
    public void payByCash(String orderId) {
        SpecialCarCashPayParam payParam = new SpecialCarCashPayParam();
        payParam.setId(orderId)
                .setMobile(getMobile())
                .setToken(getToken());

        useCase.execute(payParam,new ZMSubscriber<SpecialCarCashPayResponse>() {
                    @Override
                    public void onNext(SpecialCarCashPayResponse response) {
                        super.onNext(response);
                        if (response.isSucc()) {
                            view.handleCashPaySucceed();
                            RxBus.getInstance().send(new PayResultEvent(StatusEnum.STATUS_ENUM_TRUE));
                        } else {
                            view.handleCashPayFailure(response.getResultMessage());
                            RxBus.getInstance().send(new PayResultEvent(StatusEnum.STATUS_ENUM_FALSE));
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        view.handleCashPayError();
                        RxBus.getInstance().send(new PayResultEvent(StatusEnum.STATUS_ENUM_FALSE));
                    }
                });
    }
}
